package com.huawei;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "Cookie04", value = "/Cookie04")
public class Cookie04 extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doGet(req, resp);
        }

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setContentType("text/html;charset=UTF-8");

            PrintWriter writer = resp.getWriter();

            Cookie[] cookies = req.getCookies();
            if (cookies != null && cookies.length > 0) {
                boolean timeFlag = false;
                boolean countFlag = false;
                int loginCount = 1; // 默认为首次登录

                for (Cookie cookie : cookies) {
                    String name = cookie.getName();
                    if ("time".equals(name)) {
                        timeFlag = true;
                        String value = cookie.getValue();
                        writer.println("上次登录时间：" + value);
                    } else if ("count".equals(name)) {
                        countFlag = true;
                        loginCount = Integer.parseInt(cookie.getValue()) + 1;
                    }
                }

                if (timeFlag) {
                    Cookie timeCookie = new Cookie("time", DateTest.getTime());
                    timeCookie.setMaxAge(60 * 60 * 24);
                    resp.addCookie(timeCookie);

                    if (countFlag) {
                        Cookie countCookie = new Cookie("count", Integer.toString(loginCount));
                        countCookie.setMaxAge(60 * 60 * 24);
                        resp.addCookie(countCookie);
                        writer.println("欢迎回来，这是您第" + loginCount + "次登录");
                    } else {
                        // 如果没有找到count Cookie，创建一个新的
                        Cookie countCookie = new Cookie("count", "1");
                        countCookie.setMaxAge(60 * 60 * 24);
                        resp.addCookie(countCookie);
                        writer.println("首次登录");
                    }
                } else {
                    // 如果没有找到time Cookie，创建新的time和count Cookie
                    Cookie timeCookie = new Cookie("time", DateTest.getTime());
                    timeCookie.setMaxAge(60 * 60 * 24);
                    resp.addCookie(timeCookie);

                    Cookie countCookie = new Cookie("count", "1");
                    countCookie.setMaxAge(60 * 60 * 24);
                    resp.addCookie(countCookie);
                    writer.println("首次登录");
                }
            } else {
                // 如果没有任何Cookie，创建新的time和count Cookie
                Cookie timeCookie = new Cookie("time", DateTest.getTime());
                timeCookie.setMaxAge(60 * 60 * 24);
                resp.addCookie(timeCookie);

                Cookie countCookie = new Cookie("count", "1");
                countCookie.setMaxAge(60 * 60 * 24);
                resp.addCookie(countCookie);

                writer.println("首次登录");
            }
        }
}

